package verification;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * 验证码工厂
 * @author Administrator
 *
 */
public class VerrficationCodeFactory {
         private VerrficationCodeFactory(){}
         
         private static final char ALPHA[]={ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'G', 'K', 'M', 'N', 'P', 'Q', 'R', 'S',
     			'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'm', 'n', 'p',
    			'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '2', '3', '4', '5', '6', '7', '8', '9' };
        /**
         * 生成验证码
         */
         public static VerificationCode generateVerifictionCode(int width,int height){
        	 VerificationCode verificationCode=new VerificationCode();
        	 //创建一张图片
        	 BufferedImage image=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        	 Graphics g=image.getGraphics();
        	 Random random=new Random();
        	 //设置背景颜色
        	 g.setColor(Color.WHITE);
        	 g.fillRect(0, 0, width, height);
        	 g.setFont(new Font("Times New Roman", Font.PLAIN, 25));
        	 
        	 //随机产生100条干扰线
        	 g.setColor(getRandColor(160, 200));
     		for (int i = 0; i < 100; i++) {
     			int x = random.nextInt(width);
     			int y = random.nextInt(height);
     			int xl = random.nextInt(12);
     			int yl = random.nextInt(12);
     			g.drawLine(x, y, x + xl, y + yl);
     		}
     	// 2. 产生4位随机验证码字符
    		String code = "";
    		for (int i = 0; i < 4; i++) {
    			String rand = String.valueOf(ALPHA[random.nextInt(ALPHA.length)]);
    			code += rand;
    			// 3. 将验证码绘制到图片中
    			g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
    			g.drawString(rand, (25 * i) + 2, 25 + random.nextInt(10));
    		}
    		g.dispose();
    		
    		verificationCode.setCode(code);
    		verificationCode.setImage(image);
    		verificationCode.setKey(code);
    		return verificationCode;
        	 
         }
     	/**
     	 * 给定范围获得随机颜色
     	 * 
     	 * @param fc
     	 * @param bc
     	 * @return
     	 */
     	private static Color getRandColor(int fc, int bc) {
     		Random random = new Random();
     		if (fc > 255)
     			fc = 255;
     		if (bc > 255)
     			bc = 255;
     		int r = fc + random.nextInt(bc - fc);
     		int g = fc + random.nextInt(bc - fc);
     		int b = fc + random.nextInt(bc - fc);
     		return new Color(r, g, b);
     	}
}
